package nju.se.alibaba.algorithm;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import nju.se.alibaba.dao.AlibabaDAO;
import nju.se.alibaba.model.Tmall;

public class UserBehaviourAlgorithm extends Algorithm {

	public HashMap<Integer, HashSet<Integer>> getResults() {
		HashMap<Integer, HashSet<Integer>> result = new HashMap<Integer, HashSet<Integer>>();

		List<Integer> users = AlibabaDAO.getAllUser();
		for (int i = 0; i < users.size(); i++) {
			int user_id = users.get(i);
			System.out.println("user:"+i+":"+user_id);
			HashSet<Integer> result021 = fromType0To1(user_id);
			
			HashSet<Integer> result221 = fromType2To1(user_id);
			
			HashSet<Integer> result321 = fromType3To1(user_id);

			HashSet<Integer> result2321 = fromType2Type3To1(user_id);
			
			result021.addAll(result221);
			result021.addAll(result321);
			result021.addAll(result2321);
			
			if (result021.size()!=0) {
				result.put(user_id, result021);	
			}
			System.out.println(result021.size());
		}
		return result;
	}
	private HashSet<Integer> fromType2To1(int user_id) {
		List<Integer> tmalls = AlibabaDAO.getOnlyType2RecordByUser(user_id);
		if (tmalls == null||tmalls.size()==0) {
			return new HashSet<Integer>();
		}
		List<Integer> brands = AlibabaDAO.predictOnlyType2RecordByUser(user_id);
		if (brands == null || brands.size() == 0) {
			return new HashSet<Integer>();
		} else {
			HashSet<Integer> result = new HashSet<Integer>();
			for (int i = 0; i < brands.size(); i++) {
				result.add(brands.get(i));
			}
			return result;
		}
	}
	
	private HashSet<Integer> fromType3To1(int user_id) {
		List<Integer> tmalls = AlibabaDAO.getOnlyType3RecordByUser(user_id);
		if (tmalls == null||tmalls.size()==0) {
			return new HashSet<Integer>();
		}
		List<Integer> brands = AlibabaDAO.predictOnlyType3RecordByUser(user_id);
		if (brands == null || brands.size() == 0) {
			return new HashSet<Integer>();
		} else {
			HashSet<Integer> result = new HashSet<Integer>();
			for (int i = 0; i < brands.size(); i++) {
				result.add(brands.get(i));
			}
			return result;
		}
	}
	
	private HashSet<Integer> fromType2Type3To1(int user_id) {
		List<Integer> tmalls = AlibabaDAO.getType2Type3RecordByUser(user_id);
		if (tmalls == null||tmalls.size()==0) {
			return new HashSet<Integer>();
		}
		List<Integer> brands = AlibabaDAO.predictType2Type3RecordByUser(user_id);
		if (brands == null || brands.size() == 0) {
			return new HashSet<Integer>();
		} else {
			HashSet<Integer> result = new HashSet<Integer>();
			for (int i = 0; i < brands.size(); i++) {
				result.add(brands.get(i));
			}
			return result;
		}
	}
	
	private HashSet<Integer> fromType0To1(int user_id) {
		List<Integer> tmalls = AlibabaDAO.getOnlyType0RecordByUser(user_id);
		if (tmalls == null) {
			return new HashSet<Integer>();
		}
		Collections.sort(tmalls);
		int base_line = 0;
		switch (tmalls.size()) {
		case 0:
			return new HashSet<Integer>();
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			base_line = tmalls.get(0);
			break;
		case 6:
		case 7:
		case 8:
		case 9:
			base_line = tmalls.get(1);
			break;
		default:
			base_line = tmalls.get((int) (tmalls.size() * 0.2));
			break;
		}

		List<Integer> brands = AlibabaDAO.predictOnlyType0RecordByUser(user_id,
				base_line);
		if (brands == null || brands.size() == 0) {
			return new HashSet<Integer>();
		} else {
			HashSet<Integer> result = new HashSet<Integer>();
			for (int i = 0; i < brands.size(); i++) {
				result.add(brands.get(i));
			}
			return result;
		}

	}
}
